package org.buptsse.study;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.buptsse.ui.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private String defuri="http://rebook-photo.stor.sinaapp.com/20120320191559.jpg";
	private Context context;                            
	ArrayList<HashMap<String, String>> listItems =new ArrayList<HashMap<String,String>>(); ;    //��Ʒ��Ϣ����    
    private LayoutInflater listContainer;             
    public final class ListItemView{               
            //public ImageView image;      
            public TextView user;      
            public TextView time;
            public TextView content;    
     }      
	
    public MyAdapter(Context context, ArrayList<HashMap<String, String>> listItems) {
        this.context = context;             
        listContainer = LayoutInflater.from(context);   
        this.listItems = listItems;    
    }     
    
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItems.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public static Bitmap getImage(String Url) throws Exception {
		try {
			URL url = new URL(Url);
			String responseCode = url.openConnection().getHeaderField(0);
			if (responseCode.indexOf("200") < 0)
				throw new Exception("error" + responseCode);
			return BitmapFactory.decodeStream(url.openStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int selectID = position;    
        ListItemView  listItemView = null;    
        if (convertView == null) {    
            listItemView = new ListItemView();       
            convertView = listContainer.inflate(R.layout.postlist, null);    
            listItemView.user = (TextView)convertView.findViewById(R.id.username);    
            listItemView.time = (TextView)convertView.findViewById(R.id.time);    
            listItemView.content= (TextView)convertView.findViewById(R.id.content);    
           // listItemView.image = (ImageView)convertView.findViewById(R.id.photo);        
            convertView.setTag(listItemView);    
        }else {    
            listItemView = (ListItemView)convertView.getTag();    
        }      
        listItemView.user.setText(listItems.get(position)    
                .get("username"));    
        listItemView.time.setText(listItems.get(position).get("time"));    
        listItemView.content.setText(listItems.get(position).get("content"));    
        if(listItems.get(position).get("photo")!=null)
        {
        	try {
				//listItemView.image.setImageBitmap(getImage(listItems.get(position).get("photo")));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
  
        else
        {
        	try {
				//listItemView.image.setImageBitmap(getImage(defuri));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        return convertView;    
		
	}

}
